package com.shiguiwu.springmybatis.spring.aop.principle;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.cglib.proxy.MethodProxy;
import sun.java2d.pipe.SpanIterator;

import java.lang.reflect.Method;

/**
 * @description: 暴露到threadLocal中
 * @author: stone
 * @date: Created by 2021/6/9 16:22
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springmybatis.spring.aop.principle
 */
public class AopThreadLocalTests {

    public static void main(String[] args) {
        ProxyFactory factory = new ProxyFactory();
        Service service = new Service();
        factory.setTarget(service);

        factory.setExposeProxy(true);

        factory.addAdvice(new MethodInterceptor() {
            @Override
            public Object invoke(MethodInvocation methodInvocation) throws Throwable {

                long s = System.nanoTime();
                Object proceed = methodInvocation.proceed();
                System.out.println("执行时间是。。。。：" + (System.nanoTime() - s));
                System.out.println(proceed);
                return proceed;
            }
        });

        Service s = (Service) factory.getProxy();
        s.m1();

    }
}
